package com.sky.controller.admin;

import com.sky.dto.*;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/order")
@Slf4j
@Api(tags="订单相关接口")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("/conditionSearch")
    @ApiOperation("订单搜索")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("管理端的订单信息：{}", ordersPageQueryDTO);
        PageResult historyOrders = orderService.conditionSearch(ordersPageQueryDTO);
        return Result.success(historyOrders);
    }

    @GetMapping("/statistics")
    @ApiOperation("各个状态的订单数据统计")
    public Result<PageResult> statistics() {
        PageResult historyOrders = orderService.getStatisticsNumbers();
        return Result.success(historyOrders);
    }

    @PutMapping("/delivery/{id}")
    @ApiOperation("派送订单")
    public Result<PageResult> delivery(@PathVariable Long id) {
        OrdersConfirmDTO ordersConfirmDTO = new OrdersConfirmDTO();
        ordersConfirmDTO.setId(id);
        ordersConfirmDTO.setStatus(4);
        orderService.statusChange(ordersConfirmDTO);
        return Result.success();
    }

    @PutMapping("/confirm")
    @ApiOperation("接单")
    public Result confirmOrder(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        ordersConfirmDTO.setStatus(3);
        orderService.statusChange(ordersConfirmDTO);
        return Result.success();
    }

    @PutMapping("/complete/{id}")
    @ApiOperation("完成订单")
    public Result completeOrder(@PathVariable Long id) {
        OrdersConfirmDTO ordersConfirmDTO = new OrdersConfirmDTO();
        ordersConfirmDTO.setId(id);
        ordersConfirmDTO.setStatus(5);
        orderService.statusChange(ordersConfirmDTO);
        return Result.success();
    }

    @GetMapping("/details/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderVO> ordersDetail(@PathVariable Long id) {
        OrderVO ordersWithDetail = orderService.getOrdersWithDetail(id);
        return Result.success(ordersWithDetail);
    }

    @PutMapping("/cancel")
    @ApiOperation("取消订单")
    public Result cancelOrder(@RequestBody OrdersCancelDTO ordersCancelDTO) {
        orderService.cancelOrders(ordersCancelDTO);
        return Result.success();
    }
    @PutMapping("/rejection")
    @ApiOperation("拒绝订单")
    public Result rejectOrder(@RequestBody OrdersRejectionDTO ordersRejectionDTOD) {
        orderService.rejectOrder(ordersRejectionDTOD);
        return Result.success();
    }

}
